#ifndef __SPI
#define __SPI

#include "config.h"

typedef struct _Spi Spi;
#define SPI(any) ((Spi *)(any))
typedef enum _SpiType SpiType;

struct _Spi
{
        SPI_TypeDef  *spi;
        GPIO_TypeDef *gpio;
        uint16_t      pin_nss;
};

enum _SpiType
{
        SPI_TYPE_SPI1,
        SPI_TYPE_SPI2,
};

#define SPI_CHOOSE_NSS  0x1
#define SPI_CHOOSE_SCK  0x2
#define SPI_CHOOSE_MISO 0x4
#define SPI_CHOOSE_MOSI 0x8

/**
 * @brief SPI 对象初始化
 * @param s_type 串口号，类型为 SpiType
 * @param spi_choose 选择哪些端口启用
 */
extern void spi_init(Spi *self, SpiType s_type, uint8_t spi_choose);

/**
 * @brief 启动或关闭 SPI 总线
 */
extern void spi_start(Spi *self);
extern void spi_stop(Spi *self);

/**
 * @brief SPI 交换一个字节的数据
 */
extern uint8_t spi_send_and_receive(Spi *self, uint8_t byte);

#endif
